﻿/*
 * ReadOnlyObservableCollectionPlus
 * provides also events for CollectionChanged
 * 
 * created by: asida@centrum.cz
 * based on: http://connect.microsoft.com/VisualStudio/feedback/details/641395/readonlyobservablecollection-t-collectionchanged-event-should-be-public#details
 * 
 * initial implementation: 6.12.2011
 * last updated:
 */
using System;
using System.ComponentModel;
using System.Collections.ObjectModel;
//using System.Net;
//using System.Windows;
//using System.Collections.Generic;
using System.Collections.Specialized;



namespace System.Collections.ObjectModel
{

  /// <summary>
  /// ReadOnlyObservableCollectionPlus provides also events for CollectionChanged
  /// that is missing in MSFTs default implementation
  /// </summary>
  /// <typeparam name="T">of given type</typeparam>
  public class ReadOnlyObservableCollectionPlus<T> : ReadOnlyCollection<T>, INotifyCollectionChanged, INotifyPropertyChanged
  {



    public event NotifyCollectionChangedEventHandler CollectionChanged;
    protected event PropertyChangedEventHandler PropertyChanged;



    public ReadOnlyObservableCollectionPlus(ObservableCollection<T> list)
      : base(list)
    {
      ((INotifyCollectionChanged)Items).CollectionChanged += HandleCollectionChanged;
      ((INotifyPropertyChanged)Items).PropertyChanged += HandlePropertyChanged;
    }




    event PropertyChangedEventHandler INotifyPropertyChanged.PropertyChanged
    {
      add
      {
        PropertyChanged += value;
      }
      remove
      {
        PropertyChanged -= value;
      }
    }




    protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
    {
      var handler = CollectionChanged;
      if (handler != null)
      {
        handler(this, e);
      }
    }




    protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
    {
      var handler = PropertyChanged;
      if (handler != null)
      {
        handler(this, e);
      }
    }





    private void HandleCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
      OnCollectionChanged(e);
    }





    private void HandlePropertyChanged(object sender, PropertyChangedEventArgs e)
    {
      OnPropertyChanged(e);
    }



  }

}
